/**
 * Created by pankaj on 25/10/14.
 */
/**
 * Along with obvious parameters it takes fileShowArea.. which is used to append the newly created file input.
 */

define(['jquery','mustache', 'module/template/homeTemplate', 'module/home/uploadedFileRender'],function($,Mustache, template, renderUploadFiles){

    var fileUpload =  function( fileShowArea, fakeFileInput, fileUploadIcon){

        var thisObj = this;
        this.files = [];
        var imageHtml;

        var uploadClick = function(){
            fakeFileInput.trigger('click');
        };

        fileUploadIcon.once('click', uploadClick);

        fakeFileInput.once('change', function(event){
            var contains = false;
            $.each(thisObj.files, function(index, file){
                if(file.name === event.target.files[0].name){
                    contains = true;
                }
            });
            if(!contains){
                thisObj.files.push(event.target.files[0]);
                if(imageHtml){
                    imageHtml.remove();
                }
                imageHtml = renderUploadFiles(thisObj.files);
                fileShowArea.append(imageHtml);
            }
        });
    }
    return fileUpload;
} );
